package com.yangfan.springcloud.controller;


import com.yangfan.springcloud.entities.CommonResult;
import com.yangfan.springcloud.entities.Payment;
import com.yangfan.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.*;


/**
 * 使用RestTemplate进行远程调用
 */

@RequestMapping(value = "/order")
@RestController
@Slf4j

@EnableBinding(Sink.class)//这个注解主要是用来开启通道的
public class OrderController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping(value = "/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
        return paymentService.create(payment);
    }

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> get(@PathVariable(value = "id") Long id) {
        return paymentService.getPaymentById(id);
    }


    @GetMapping(value = "/consumer/payment/timeout")
    public CommonResult timeout() {
        System.out.println("timeout");
        return paymentService.timeout();
    }


    /**
     * 消费者监听 某个消息中间件 里面的消息
     * @param message
     */
    @StreamListener(Sink.INPUT)//监听消息中间件的消息 如果有消息 就会调用这个方法 进行消费
    public void input(Message<String> message) {
        log.info(message.getPayload());
    }


}
